﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Employees : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        handleEditMode();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (btnSave.CommandArgument == "")
        {
            tbl_employee_master model = new tbl_employee_master();
            model.USER_CODE = ddlUSER_CODE.SelectedValue;
            model.USER_ROLE_ID = Convert.ToInt32(ddlUSER_ROLE_ID.SelectedValue.Trim());
            model.FIRST_NAME = txtFIRST_NAME.Text.Trim();
            model.LAST_NAME = txtLAST_NAME.Text.Trim();
            model.DEPARTMENT_ID = Convert.ToInt32(ddlDEPARTMENT_ID.SelectedValue.Trim());
            model.DESIGNATION_ID = Convert.ToInt32(ddlDESIGNATION_ID.SelectedValue.Trim());
            model.CREATED_BY = User.Identity.Name;
            model.CREATED_DATE = DateTime.Now;



            BLL_tbl_employee_master bll = new BLL_tbl_employee_master();
            try
            {



                bll.insert(model);
                string script = "top.location=" + '"' + HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + Request.RawUrl + '"';
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "alert('Record Saved Succesfully');", true);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Postback", script, true);

                return;
            }
            catch (Exception)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured');", true);
                return;
            }
        }
        else
        {
            tbl_employee_master model = new tbl_employee_master();
            model.USER_CODE = btnSave.CommandArgument.Trim();
            model.USER_CODE = ddlUSER_CODE.SelectedValue;
            model.USER_ROLE_ID = Convert.ToInt32(ddlUSER_ROLE_ID.SelectedValue.Trim());
            model.FIRST_NAME = txtFIRST_NAME.Text.Trim();
            model.LAST_NAME = txtLAST_NAME.Text.Trim();
            model.DEPARTMENT_ID = Convert.ToInt32(ddlDEPARTMENT_ID.SelectedValue.Trim());
            model.DESIGNATION_ID = Convert.ToInt32(ddlDESIGNATION_ID.SelectedValue.Trim());
            model.UPDATED_BY = User.Identity.Name;
            model.UPDATED_DATE = DateTime.Now;



            BLL_tbl_employee_master bll = new BLL_tbl_employee_master();
            bll.update(model);
            btnSave.CommandArgument = "";
            Response.Redirect(Request.Url.AbsolutePath);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }

    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        tbl_employee_master model = new tbl_employee_master();
        model.USER_CODE = id.ToString();

        BLL_tbl_employee_master bll = new BLL_tbl_employee_master();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }


    protected void handleEditMode()
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["editRecord"] != null)
            {
                string id = Request.QueryString["editRecord"];
                BLL_tbl_employee_master bll = new BLL_tbl_employee_master();
                List<tbl_employee_master> list = bll.select(id);
                foreach (tbl_employee_master tbl in list)
                {
                    btnSave.CommandArgument = tbl.USER_CODE.ToString();
                    ddlUSER_CODE.SelectedValue = tbl.USER_CODE;
                    ddlUSER_ROLE_ID.SelectedValue = tbl.USER_ROLE_ID.ToString();
                    txtFIRST_NAME.Text = tbl.FIRST_NAME;
                    txtLAST_NAME.Text = tbl.LAST_NAME;
                    ddlDEPARTMENT_ID.SelectedValue = tbl.DEPARTMENT_ID.ToString();
                    ddlDESIGNATION_ID.SelectedValue = tbl.DESIGNATION_ID.ToString();
                }
            }
        }
    }


                
}